﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HP.Cachalote.Services.Shared
{
    public static class GeneralUtilities
    {
        /// <summary>
        /// Return current system datetime.
        /// </summary>
        /// <returns>Current system datetime.</returns>
        public static DateTime GetCurrentDateTime()
        {
            return DateTime.Now;
        }

        /// <summary>
        /// Get the date of the Saturday based on provided date.
        /// </summary>
        /// <param name="date">The provided date</param>
        /// <returns>The date of the Saturday.</returns>
        public static DateTime GetDateOfSaturday(DateTime date)
        {
            return date.AddDays(DayOfWeek.Saturday - date.DayOfWeek);
        }

        /// <summary>
        /// Get the date of the next Saturday based on provided date.
        /// </summary>
        /// <param name="date">The provided date</param>
        /// <returns>The date of the next Saturday.</returns>
        public static DateTime GetDateOfNextSaturday(DateTime date)
        {
            return GetDateOfSaturday(date).AddDays(7);
        }

        /// <summary>
        /// Check if the provided two dates are in the same year.
        /// </summary>
        /// <param name="dateA">The provided date.</param>
        /// <param name="dateB">The other of provided date.</param>
        /// <returns>True means the two provided dates are in the same year. False means NOT.</returns>
        public static bool CheckIsSameYear(DateTime dateA, DateTime dateB)
        {
            if (dateA.Year == dateB.Year)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Check if the provided two dates are in the same year & month.
        /// </summary>
        /// <param name="dateA">The provided date.</param>
        /// <param name="dateB">The other of provided date.</param>
        /// <returns>True means the two provided dates are in the same year & month. False means NOT.</returns>
        public static bool CheckIsSameYearAndMonth(DateTime dateA, DateTime dateB)
        {
            if ((CheckIsSameYear(dateA, dateB)) && dateA.Month == dateB.Month)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Check if the nextMonthDate is in the same year & next month of date.
        /// </summary>
        /// <param name="date">The provided date.</param>
        /// <param name="nextMonthDate">The other of provided date. This might be in the next month of the first date.</param>
        /// <returns>True means the nextMonthDate is in the same year & next month of date. False means NOT.</returns>
        public static bool CheckIsSameYearAndNextMonth(DateTime date, DateTime nextMonthDate)
        {
            if (date.Year == nextMonthDate.Year && date.AddMonths(1).Month == nextMonthDate.Month)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Return the short name of the spcified month.
        /// </summary>
        /// <param name="month">The specified month.</param>
        /// <returns>The short name of the spcified month.</returns>
        public static string GetMonthShortName(int month)
        {
            var shortName = string.Empty;

            switch (month)
            {
                case 1:
                    shortName = "Jan";
                    break;
                case 2:
                    shortName = "Feb";
                    break;
                case 3:
                    shortName = "Mar";
                    break;
                case 4:
                    shortName = "Apr";
                    break;
                case 5:
                    shortName = "May";
                    break;
                case 6:
                    shortName = "Jun";
                    break;
                case 7:
                    shortName = "Jul";
                    break;
                case 8:
                    shortName = "Aug";
                    break;
                case 9:
                    shortName = "Sep";
                    break;
                case 10:
                    shortName = "Oct";
                    break;
                case 11:
                    shortName = "Nov";
                    break;
                case 12:
                    shortName = "Dec";
                    break;
            }

            return shortName;
        }

        /// <summary>
        /// Return the oclock time based on provided datetime. 
        /// </summary>
        /// <param name="dateTime">The provided datetime.</param>
        /// <returns>If the time is 21:02 then will return 21:30. If the time is 21:32 then will return 22:00.</returns>
        public static DateTime GetOclockTime(DateTime dateTime)
        {
            if (dateTime.Minute < 30)
            {
                // If the time is 21:02 then will return 21:30.
                return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 30, 0);
            }
            else
            {
                // If the time is 21:32 then will return 22:00.
                var newDateTime = dateTime.AddHours(1);
                return new DateTime(newDateTime.Year, newDateTime.Month, newDateTime.Day, newDateTime.Hour, 0, 0);
            }
        }

        /// <summary>
        /// Get filter type based on provided item's DueDate.
        /// </summary>
        /// <param name="item">The item's DueDate.</param>
        /// <returns>TodoItemFilterType. It includes Today, Tomorrow, Next, Overdue.</returns>
        public static TodoItemFilterType GetFilterType(DateTime? dueDate)
        {
            // If The DueDate is null then this item will belong to the filter called Next.
            var filterType = TodoItemFilterType.Next;

            if (dueDate.HasValue)
            {
                if (dueDate.Value.Date == GeneralUtilities.GetCurrentDateTime().Date)
                {
                    filterType = TodoItemFilterType.Today;
                }
                else if (dueDate.Value.Date == GeneralUtilities.GetCurrentDateTime().AddDays(1).Date)
                {
                    filterType = TodoItemFilterType.Tomorrow;
                }
                else if (dueDate.Value.Date > GeneralUtilities.GetCurrentDateTime().AddDays(1).Date)
                {
                    filterType = TodoItemFilterType.Next;
                }
                else if (dueDate.Value.Date < GeneralUtilities.GetCurrentDateTime().Date)
                {
                    filterType = TodoItemFilterType.Overdue;
                }
            }

            return filterType;
        }
    }
}
